package com.pcy.codegenerator.config;

import com.pcy.codegenerator.utils.annotation.DataSourceTarget;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class DataSourceAspect {
    @Before("@annotation(ds)")
    public void beforeDataSource(DataSourceTarget ds) {
        String value = ds.value().getDb();
        DataSourceContextHolder.setDataSource(value);
        log.info("当前使用的数据源为：{}", value);
    }
    @After("@annotation(ds)")
    public void afterDataSource(DataSourceTarget ds){
        DataSourceContextHolder.clearDataSource();
    }
}